package br.com.unip.visao;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import br.com.unip.modelo.Participa;
import br.com.unip.persistencia.ParticipaDao;

public class AppParticipaNovoGui extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog dialogFunc;
	private JPanel panelPartic;
	private JLabel labelPartCodProj, labelPartCodFunc, labelPartHorasTrab;
	private JTextField textPartCodProj, textlPartCodFunc, textPartHorasTrab;
	private JButton btnPartSalvar, btnPartCancelar;

	public AppParticipaNovoGui() {
		super("Projeto UNIP - SWING - ALPOO - Novo participante");
		
		labelPartCodProj = new JLabel("Código do projeto:");
		labelPartCodFunc = new JLabel("Código do funcionario:");
		labelPartHorasTrab = new JLabel("Horas trabalhadas:");

		textPartCodProj = new JTextField(50);
		textlPartCodFunc = new JTextField(50);
		textPartHorasTrab = new JTextField(50);

		btnPartSalvar = new JButton("Salvar");
		btnPartCancelar = new JButton("Cancelar");

		btnPartSalvar.addActionListener(this);
		btnPartCancelar.addActionListener(this);

		panelPartic = new JPanel();
		panelPartic.setLayout(new GridLayout(6,2));
		panelPartic.setBorder(BorderFactory.createTitledBorder("Dados"));
		panelPartic.add(labelPartCodProj);
		panelPartic.add(textPartCodProj);
		panelPartic.add(labelPartCodFunc);
		panelPartic.add(textlPartCodFunc);
		panelPartic.add(labelPartHorasTrab);
		panelPartic.add(textPartHorasTrab);
		panelPartic.add(btnPartSalvar);
		panelPartic.add(btnPartCancelar);
		
		dialogFunc = new JDialog();
		dialogFunc.add(panelPartic);
		
		dialogFunc.setResizable(false);
		dialogFunc.setSize(650, 200);
		dialogFunc.setLocationRelativeTo(null);
		dialogFunc.setModal(true);
		dialogFunc.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae) {
		Object evento = ae.getSource();
		if (evento == btnPartSalvar) {
			System.out.println("Ação => Salvando");
			try {
				Participa p = new Participa();
				ParticipaDao pd = new ParticipaDao();
				p.setCod_proj(Integer.parseInt(textPartCodProj.getText()));
				p.setCod_func(Integer.parseInt(textlPartCodFunc.getText()));
				p.setHoras_trab(Float.parseFloat(textPartHorasTrab.getText()));
				pd.gravar(p);
				textPartCodProj.setText("");
				textlPartCodFunc.setText("");
				textPartHorasTrab.setText("");
				JOptionPane.showMessageDialog(null,
						"Registro salvo com sucesso!", "Sucesso",
						JOptionPane.INFORMATION_MESSAGE);
				
				// Gambiarra que tem que ser arrumada! o.O (sem net para pesquisar)
				AppListGui.paneLista.setSelectedIndex(2);
				AppListGui.paneLista.setSelectedIndex(3);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Erro : " + e.getMessage(),
						"Atenção!", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (evento == btnPartCancelar) {
			System.out.println("Ação => Cancelando");
			dialogFunc.dispose();
		}
	}
}
